<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/shine') // echarts theme
// import resize from 'mixins/resize'

export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'shine')
      this.setOptions(this.chartData)
    },
    setOptions({ batteryData } = {}) {
      this.chart.setOption({
        color: ['#0ddb5d'],
        tooltip: {
          trigger: 'axis',
          axisPointer: {            // 坐标轴指示器，坐标轴触发有效
            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            show: false,
            type: 'category',
            data: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'],
            axisTick: {
              alignWithLabel: true
            },
            splitLine: {
              show: false
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '电压',
            type: 'bar',
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(220,211,218,0.8)'
            },
            barWidth: '70%',
            data: batteryData,
            itemStyle: {
              normal: {
                color: function(params) {
                  if (params.value < 10) {
                    return '#db1e04'
                  } else {
                    return '#0ddb5d'
                  }
                }
              }
            }
          }
        ]
      })
    }
  }
}
</script>
